package com.qf.work20250111;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author zdl
 * @date 2025/1/11
 */
public class Demo2 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //反射  获取构造方法
        Class<Person> pc = Person.class;
        //获取无参构造
        Constructor<Person> constructor = pc.getConstructor(null);
        System.out.println(constructor);
        Person person = constructor.newInstance();
        System.out.println(person);

        System.out.println("---------------");
        //有参构造  String name
        Constructor<Person> constructor1 = pc.getConstructor(String.class);
        System.out.println(constructor1);
        Person person1 = constructor1.newInstance("王五");
        System.out.println(person1);


        //String name,int age 有参构造
        System.out.println("--------------");
        Constructor<Person> constructor2 = pc.getConstructor(String.class, int.class);
        System.out.println(constructor2);
        Person person2 = constructor2.newInstance("Jack", 20);
        System.out.println(person2);
    }
}
